﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.ZhaoLuService;
using org.in2bits.MyXls;

namespace CommunitySystem
{
    public partial class TiJianControl_ZLGL : UserControl
    {
        private ZhaoLuService.ZhaoLuServiceClient Client1 = new ZhaoLuServiceClient();

        private ObservableCollection<ZhaoLu_DaoRuKaoShengModel> selectedempmodel =
            new ObservableCollection<ZhaoLu_DaoRuKaoShengModel>();
        public TiJianControl_ZLGL()
        {
            InitializeComponent();
            for (int i = DateTime.Today.Year; i > DateTime.Today.Year - 10; i--)
            {
                ComboBoxItem cbi = new ComboBoxItem();
                cbi.Content = i.ToString();
                cbi.Tag = i;
                nian.Items.Add(cbi);
            }
            nian.SelectedIndex = 0;
            Client1.GetZhaoLu_DaoRuKaoShengModelCompleted += new EventHandler<GetZhaoLu_DaoRuKaoShengModelCompletedEventArgs>(Client1_GetZhaoLu_DaoRuKaoShengModelCompleted);
            Client1.QiQuanCompleted += new EventHandler<QiQuanCompletedEventArgs>(Client1_QiQuanCompleted);
            Client1.UpdateZL_TiJianCompleted += new EventHandler<UpdateZL_TiJianCompletedEventArgs>(Client1_UpdateZL_TiJianCompleted);
            Load();
        }

       

        public void Load()
        {
            Client1.GetZhaoLu_DaoRuKaoShengModelAsync(DateTime.Today.Year - nian.SelectedIndex, false, yue.SelectedIndex + 1, 0);
        }
        private ObservableCollection<ZhaoLu_DaoRuKaoShengModel> temp =
            new ObservableCollection<ZhaoLu_DaoRuKaoShengModel>();
        void Client1_GetZhaoLu_DaoRuKaoShengModelCompleted(object sender, GetZhaoLu_DaoRuKaoShengModelCompletedEventArgs e)
        {
            //temp = (ObservableCollection<ZhaoLu_DaoRuKaoShengModel>)e.Result;
            //List<string> tempstring = new List<string>();
            //if (temp != null && temp.Count > 0)
            //{
            //    for (int i = 0; i < temp.Count; i++)
            //    {
            //        if (!tempstring.Contains(temp[i].YRBM + "-" + temp[i].BBZW))
            //            tempstring.Add(temp[i].YRBM + "-" + temp[i].BBZW);

            //    }
            //    for (int i = 0; i < tempstring.Count; i++)
            //    {
            //        for (int n = 0; n < temp.Where(p => p.YRBM == tempstring[i].Split('-')[0] &&
            //                                                            p.BBZW == tempstring[i].Split('-')[1]).Count(); n++)
            //        {
            //            temp.Where(p => p.YRBM == tempstring[i].Split('-')[0] &&
            //                            p.BBZW == tempstring[i].Split('-')[1]).ToList()[n].MingCi = n + 1;
            //        }
            //    }
            //}

            ////for (int i = 0; i < temp.Count; i++)
            ////{
            ////    temp[i].Sec = i + 1;
            ////}
            DGRole.ItemsSource = null;
            DGRole.ItemsSource = e.Result;
        }

        private void Data_Click(object sender, RoutedEventArgs e)
        {
            if (DGRole.ItemsSource != null)
            {
                foreach (ZhaoLu_DaoRuKaoShengModel model in (ObservableCollection<ZhaoLu_DaoRuKaoShengModel>)DGRole.ItemsSource)
                {
                    Client1.UpdateZL_TiJianAsync(model);
                    count = count + 1;
                }
            }
        }
        private int count = 0;
        void Client1_UpdateZL_TiJianCompleted(object sender, UpdateZL_TiJianCompletedEventArgs e)
        {
            if (count == ((ObservableCollection<ZhaoLu_DaoRuKaoShengModel>)DGRole.ItemsSource).Count)
            {
                count = count - 1;
                SilverlightMessageBoxLibrary.Message.InfoMessage("保存成功!");
                Load();
            }
        }

        private void fangqi_Click(object sender, RoutedEventArgs e)
        {
            if (DGRole.ItemsSource != null)
            {
                foreach (ZhaoLu_DaoRuKaoShengModel i in (ObservableCollection<ZhaoLu_DaoRuKaoShengModel>)DGRole.ItemsSource)
                {
                    if (i.IsSelected == true && selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() == null)
                    {
                        selectedempmodel.Add(i);
                    }
                    if (selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() != null && i.IsSelected == false)
                    {
                        selectedempmodel.Remove(selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault());
                    }
                }
            }
            if (selectedempmodel.Count > 0)
            {
                if (MessageBox.Show("确定进行弃权操作吗？", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    string ss = "";
                    foreach (ZhaoLu_DaoRuKaoShengModel i in selectedempmodel)
                    {
                        ss += i.ID.ToString() + ",";
                    }
                    Client1.QiQuanAsync(ss.Substring(0, ss.Length - 1), "体检");
                }

            }
        }
        void Client1_QiQuanCompleted(object sender, QiQuanCompletedEventArgs e)
        {
            MessageBox.Show("弃权成功！");
            Load();
        }
        private void ChaData_Click(object sender, RoutedEventArgs e)
        {
            Load();
        }
        private void CheckBox_Click(object sender, RoutedEventArgs e)
        {
            CheckBox cb = sender as CheckBox;
            foreach (ZhaoLu_DaoRuKaoShengModel p in DGRole.ItemsSource)
            {
                p.IsSelected = (bool)cb.IsChecked;
                if (p.IsSelected)
                {
                    if (selectedempmodel.Where(i => i.ID == p.ID).FirstOrDefault() == null)
                    {
                        selectedempmodel.Add(p);
                    }
                }
                else
                {
                    if (selectedempmodel.Where(i => i.ID == p.ID).FirstOrDefault() != null)
                    {
                        selectedempmodel.Remove(selectedempmodel.Where(i => i.ID == p.ID).FirstOrDefault());
                    }
                }
            }
        }
        private Stream ss = null;
        private void SaveData_Click(object sender, RoutedEventArgs e)
        {
            if (DGRole.ItemsSource != null)
            {
                foreach (ZhaoLu_DaoRuKaoShengModel i in (ObservableCollection<ZhaoLu_DaoRuKaoShengModel>)DGRole.ItemsSource)
                {
                    if (i.IsSelected == true && selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() == null)
                    {
                        selectedempmodel.Add(i);
                    }
                    if (selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() != null && i.IsSelected == false)
                    {
                        selectedempmodel.Remove(selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault());
                    }
                }
            }
            if (selectedempmodel.Count > 0)
            {
                var downLoad = new SaveFileDialog()
                {
                    //Filter = "Excel files(2007~2010) (*.xlsx)|*.xlsx|Excel files(2003) (*.xls)|*.xls", //"Excel files (*.xlsx)|*.xlsx|(*.xls)|*.xls",
                    Filter = "Excel files(2003) (*.xls)|*.xls",
                };
                if (downLoad.ShowDialog() == true)
                {
                    ss = downLoad.OpenFile();
                    PageInit();
                }
            }
        }
        public void PageInit()
        {
            XlsDocument xls = new XlsDocument();
            XF xf = xls.NewXF();
            xf.HorizontalAlignment = HorizontalAlignments.Centered;
            xf.VerticalAlignment = VerticalAlignments.Centered;
            xf.UseBorder = true;
            xf.TopLineStyle = 1;
            xf.TopLineColor = org.in2bits.MyXls.Colors.Black;
            xf.BottomLineStyle = 1;
            xf.BottomLineColor = org.in2bits.MyXls.Colors.Black;
            xf.LeftLineStyle = 1;
            xf.LeftLineColor = org.in2bits.MyXls.Colors.Black;
            xf.RightLineStyle = 1;
            xf.RightLineColor = org.in2bits.MyXls.Colors.Black;
            xf.TextWrapRight = true;

            XF xf1 = xls.NewXF();
            xf1.UseBorder = true;
            xf1.TopLineStyle = 1;
            xf1.TopLineColor = org.in2bits.MyXls.Colors.Black;
            xf1.BottomLineStyle = 1;
            xf1.BottomLineColor = org.in2bits.MyXls.Colors.Black;
            xf1.LeftLineStyle = 1;
            xf1.LeftLineColor = org.in2bits.MyXls.Colors.Black;
            xf1.RightLineStyle = 1;
            xf1.RightLineColor = org.in2bits.MyXls.Colors.Black;
            xf1.HorizontalAlignment = HorizontalAlignments.Centered;
            xf1.VerticalAlignment = VerticalAlignments.Centered;
            xf1.Font.Bold = true;
            xf1.Font.Height = 15 * 20;

            XF xf3 = xls.NewXF();
            xf3.UseBorder = true;
            xf3.TopLineStyle = 1;
            xf3.TopLineColor = org.in2bits.MyXls.Colors.Black;
            xf3.BottomLineStyle = 1;
            xf3.BottomLineColor = org.in2bits.MyXls.Colors.Black;
            xf3.LeftLineStyle = 1;
            xf3.LeftLineColor = org.in2bits.MyXls.Colors.Black;
            xf3.RightLineStyle = 1;
            xf3.RightLineColor = org.in2bits.MyXls.Colors.Black;
            xf3.HorizontalAlignment = HorizontalAlignments.Centered;
            xf3.VerticalAlignment = VerticalAlignments.Centered;
            xf3.Font.Bold = true;
            xf3.Font.Height = 12 * 20;

            Worksheet sheet1 = xls.Workbook.Worksheets.AddNamed("人员体检表");
            sheet1.Cells.Merge(1, 1, 1, 7);
            sheet1.Cells.Add(1, 1, "徐汇区" + (DateTime.Today.Year - nian.SelectedIndex).ToString() + "年招录人员体检表", xf1);
            sheet1.Cells.Add(2, 1, "序号", xf3);
            sheet1.Cells.Add(2, 2, "名次", xf3);
            sheet1.Cells.Add(2, 3, "姓名", xf3);
            sheet1.Cells.Add(2, 4, "身份证号", xf3);
            sheet1.Cells.Add(2, 5, "报名单位", xf3);
            sheet1.Cells.Add(2, 6, "报名职务", xf3);
            sheet1.Cells.Add(2, 7, "体检结果", xf3);
            //sheet1.Cells.Add(2, 7, "笔试分数", xf3);
            //sheet1.Cells.Add(2, 8, "面试分数", xf3);
            //sheet1.Cells.Add(2, 9, "总分数", xf3);

            ColumnInfo colInfoxueli1 = new ColumnInfo(xls, sheet1);
            colInfoxueli1.Width = 15 * 256;
            colInfoxueli1.ColumnIndexStart = 3;
            colInfoxueli1.ColumnIndexEnd = 3;
            sheet1.AddColumnInfo(colInfoxueli1);

            ColumnInfo colInfoxueli2 = new ColumnInfo(xls, sheet1);
            colInfoxueli2.Width = 25 * 256;
            colInfoxueli2.ColumnIndexStart = 4;
            colInfoxueli2.ColumnIndexEnd = 4;
            sheet1.AddColumnInfo(colInfoxueli2);

            ColumnInfo colInfoxueli3 = new ColumnInfo(xls, sheet1);
            colInfoxueli3.Width = 20 * 256;
            colInfoxueli3.ColumnIndexStart = 5;
            colInfoxueli3.ColumnIndexEnd = 5;
            sheet1.AddColumnInfo(colInfoxueli3);

            for (int i = 0; i < selectedempmodel.Count; i++)
            {
                sheet1.Cells.Add(i + 3, 1, i + 1, xf);
                sheet1.Cells.Add(i + 3, 2, selectedempmodel[i].MingCi.ToString(), xf);
                if (selectedempmodel[i].XingMing != null) sheet1.Cells.Add(i + 3, 3, selectedempmodel[i].XingMing.ToString(), xf);
                if (selectedempmodel[i].SFZH != null) sheet1.Cells.Add(i + 3, 4, selectedempmodel[i].SFZH.ToString(), xf);
                if (selectedempmodel[i].YRBM != null) sheet1.Cells.Add(i + 3, 5, selectedempmodel[i].YRBM.ToString(), xf);
                if (selectedempmodel[i].BBZW != null) sheet1.Cells.Add(i + 3, 6, selectedempmodel[i].BBZW.ToString(), xf);
                if (selectedempmodel[i].TiJianResult != null) sheet1.Cells.Add(i + 3, 7, selectedempmodel[i].TiJianResult.ToString(), xf);
            }
            xls.Save(ss);
            ss.Close();
            ss.Dispose();
            SilverlightMessageBoxLibrary.Message.InfoMessage("导出成功！");
        }
    }
}
